package homework;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//作业标题(2732)
//二叉树前序非递归遍历
//
//作业内容
//二叉树前序非递归遍历
//https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
public class h2732 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root == null) return ret;

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode cur = stack.pop();
            ret.add(cur.val);
            if(cur.right != null) stack.push(cur.right);
            if(cur.left != null) stack.push(cur.left);
        }

        return ret;
    }
}
